package com.anXin.article.service.impl;

import com.anXin.article.mapper.ReplyChildMapper;
import com.anXin.article.mapper.ReplyMapper;
import com.anXin.article.service.ReplyService;
import com.anXin.model.article.pojos.Reply;
import com.anXin.model.article.pojos.ReplyChild;
import com.anXin.model.common.dtos.ResponseResult;
import com.anXin.model.common.dtos.article.ServiceCode;
import com.anXin.model.common.dtos.article.ServiceException;
import com.anXin.model.common.enums.AppHttpCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class ReplyServiceImpl extends ServiceImpl<ReplyMapper, Reply> implements ReplyService {
    @Resource
    ReplyMapper mapper;
    @Resource
    ReplyChildMapper childMapper;
    @Override
    public ResponseResult deleteReply(Long id){
        Reply reply = mapper.selectById(id);
        if (reply!=null){
            QueryWrapper<ReplyChild> wrapper = new QueryWrapper<>();
            List<ReplyChild> lists = childMapper.selectList(wrapper.eq("parent_id",id));
            if (!lists.isEmpty()){
                for (ReplyChild list : lists){
                    childMapper.deleteById(list.getId());
                }
            }
           mapper.deleteById(id);
        }else throw new ServiceException(ServiceCode.ERROR,"评论不存在");
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }

}
